#include "record_cache.h"
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "basic_time.h"
#include "common_log.h"

int RCache_GetInterval(const char *oldtime, const char *newtime) {
	time_t rawold, rawnew;
	struct tm tmold, tmnew;
	int interval;
	strptime(oldtime, "%Y%m%d%H%M%S", &tmold);
	strptime(newtime, "%Y%m%d%H%M%S", &tmnew);
	rawold = mktime(&tmold);
	rawnew = mktime(&tmnew);
	interval = (int)(rawnew-rawold);
	return interval;
}

void RCache_AddInterval(const char *time, const char *interval, char *outTime, int outSize) {
	time_t rawtime, rawtout;
	struct tm tmtime, tmtout;
	int rawinterval = atoi(interval);
	strptime(time, "%Y%m%d%H%M%S", &tmtime);
	rawtime = mktime(&tmtime);
	rawtout = rawtime + rawinterval;
	memcpy(&tmtout, localtime(&rawtout), sizeof(struct tm));
	strftime(outTime, outSize, "%Y%m%d%H%M%S", &tmtout);
}

void RCache_Read(const char *confPath, t_RecordCache *recordCache) {
	GetPrivateProfileString("download", "time", "0", recordCache->time, sizeof(recordCache->time), confPath);
	GetPrivateProfileString("download", "interval", "0", recordCache->interval, sizeof(recordCache->interval), confPath);
}

void RCache_Write(const char *confPath, const t_RecordCache *recordCache) {
	WritePrivateProfileString("download", "time", recordCache->time, confPath);
	WritePrivateProfileString("download", "interval", recordCache->interval, confPath);
}

void RCache_Print(const t_RecordCache *recordCache) {
	printf("[download]time=%s, interval=%s\n", recordCache->time, recordCache->interval);
	LOGINFO("[download]time=%s, interval=%s\n", recordCache->time, recordCache->interval);
}

#if 0
int main(void) {
	char confPath[256] = "F:\\Sang_Yang\\15_HeBi\\HeBi_Ftpget\\bin\\record.cache";
	t_RecordCache recordCache, recCacheOut;
	t_SepTime sepTime;

	RCache_Read(confPath, &recordCache);
	RCache_SepTime(recordCache.time, &sepTime);
	RCache_AddInterval(recordCache.time, recordCache.interval, recCacheOut.time, sizeof(recCacheOut.time));
	strcpy(recCacheOut.interval, recordCache.interval);
	RCache_Write(confPath, &recCacheOut);
	RCache_Print(&recordCache);

	return 0;
}
#endif

